package com.zby.zbyliving.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zby.zbyliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.zby.zbyliving.commodity.entity.AttrEntity;
import com.zby.zbyliving.commodity.service.AttrAttrgroupRelationService;
import com.zby.zbyliving.commodity.service.AttrService;
import com.zby.zbyliving.commodity.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zby.zbyliving.commodity.entity.AttrgroupEntity;
import com.zby.zbyliving.commodity.service.AttrgroupService;
import com.zby.common.utils.PageUtils;
import com.zby.common.utils.R;

import javax.annotation.Resource;


/**
 * 家居商品属性分组
 *
 * @author zby
 * @email 2069189112@qq.com
 * @date 2023-07-26 18:40:47
 */
@Slf4j
@RestController
@RequestMapping("commodity/attrgroup")
public class AttrgroupController {
    @Resource
    private AttrgroupService attrgroupService;
    @Resource
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrgroupService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId) {
        PageUtils page = attrgroupService.queryPage(params, categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("commodity:attrgroup:info")
    public R info(@PathVariable("id") Long id){
		AttrgroupEntity attrgroup = attrgroupService.getById(id);
        Long categoryId = attrgroup.getCategoryId();
        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(categoryId);
        attrgroup.setCascadedCategoryId(cascadedCategoryId);
        return R.ok().put("attrgroup", attrgroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("commodity:attrgroup:save")
    public R save(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.save(attrgroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("commodity:attrgroup:update")
    public R update(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.updateById(attrgroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("commodity:attrgroup:delete")
    public R delete(@RequestBody Long[] ids){
		attrgroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    @Resource
    AttrService attrService;
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrAttrgroupRelationEntity[]
                                    attrAttrgroupRelationEntities){
        attrService.deleteRelation(attrAttrgroupRelationEntities);
        return R.ok();
    }

    @RequestMapping("/{attrgroupId}/attr/allowrelation")
    public R attrAllowRelation(@PathVariable("attrgroupId") Long attrgroupId,
                               @RequestParam Map<String, Object> params) {
        PageUtils page =
                attrService.getAllowRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities) {

        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationEntities);
        return R.ok();
    }

}
